package cn.b.sky.mes.em.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.em.dao.ShutDownCauseDao;
import cn.b.sky.mes.em.model.ShutDownCause;
import cn.b.sky.mes.em.model.condition.ShutDownCauseCondition;
import cn.b.sky.mes.em.service.ShutDownCauseService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

@Service("shutDownCauseService")
public class ShutDownCauseServiceImpl implements ShutDownCauseService {

  private static final Logger LOG = LoggerFactory.getLogger(ShutDownCauseServiceImpl.class);

  @Resource
  ShutDownCauseDao shutDownCauseDao;

  @Override
  public List<ShutDownCause> selectShutDownCauseListByCondition(ShutDownCauseCondition condition) {
    if (condition == null) {
      condition = new ShutDownCauseCondition();
    }
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("CreateDateTime");
    }
    return shutDownCauseDao.selectShutDownCauseListByCondition(condition);
  }

  @Override
  public int selectShutDownCauseCount(ShutDownCauseCondition condition) {
    if (condition == null) {
      condition = new ShutDownCauseCondition();
    }
    return shutDownCauseDao.selectShutDownCauseCount(condition);
  }

  @Override
  public void updateShutDownCause(ShutDownCause shutDownCause) {
    if (shutDownCause == null || StringUtils.isBlank(shutDownCause.getShutDownCauseCode())) {
      return;
    }
    shutDownCauseDao.updateShutDownCause(shutDownCause);

  }

  @Override
  public void saveShutDownCause(ShutDownCause shutDownCause) {
    if (shutDownCause == null || StringUtils.isBlank(shutDownCause.getShutDownCauseCode())) {
      return;
    }
    shutDownCauseDao.saveShutDownCause(shutDownCause);

  }

  @Override
  public BatchJobReport deleteShutDownCause(String shutDownCauseCode, String siteCode) {
    if (!StringUtils.isBlank(shutDownCauseCode)) {
      String message = shutDownCauseCode;
      String cause = "";
      boolean success = true;
      try {
        shutDownCauseDao.deleteShutDownCause(shutDownCauseCode, siteCode);
      } catch (Exception e) {
        LOG.error("{}", e);
        success = false;
        String errorMsg = e.getMessage();
        if (DatabaseUtils.isConstraintsError(e.getMessage())) {
          //外键删除已存在提示
          errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
              SecurityContextUtil.getLanguageCode(), "dataBaseOperationPkFail.M")
              + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
                  SecurityContextUtil.getLanguageCode(), "notDelete.M");
        }
        cause = errorMsg;
      }
      return new BatchJobReport(message, cause, success);
    } else {
      return new BatchJobReport("", "shutDownCause is null", false);
    }
  }

  @Override
  public ShutDownCause getShutDownCause(String causeCode, String siteCode) {
    if (StringUtils.isBlank(causeCode)) {
      return null;
    }
    return shutDownCauseDao.getShutDownCause(causeCode, siteCode);
  }

  @Override
  public List<ShutDownCause> selectShutDownCauseForSuggest(ShutDownCauseCondition condition) {
    return shutDownCauseDao.selectShutDownCauseForSuggest(condition);
  }

}
